var app = getApp();
const pathUrl = app.globalData.pathUrl;
Page({
    data: {
        pathUrl: pathUrl,
        openId: '',
        proNo: '',
        orderNo: '',
        proIsdb: '', //是否有担保人1是
        proAttList: [], //上传附件列表
        array: ['12', '18', '24', '36', '48'],
      // array: ['36个月'],
        flag: true,
        ocrName: '', //ocr姓名
        ocrSex: '', //ocr性别
        ocrNation: '', //ocr民族
        ocrBirth: '', //ocr出生
        ocrAddress: '', //ocr地址
        ocrIdNumber: '', //ocr身份证号
        ocrOffice: '', //ocr签发机关
        ocrValidityPeriod: '', //ocr有效期限
        cusIdValidfrom: '', //证件开始日期
        cusIdValidto: '', //证件结束日期
        orderBy00: '',
        orderBy01: '',
        orderBy02: '',
        orderBy03: '',
        orderBy04: '',
        orderBy05: '',
        orderBy06: '',
        orderBy07: '',
        orderBy08: '',
      orderBy4: '',
      orderBy5: '',
      orderBy6: '',
      orderBy7: '',
      orderBy8: '',
      orderBy9: '',
      orderBy10: '',
      orderBy11: '',
      orderBy12: '',
      orderBy13: '',
      orderBy14: '',
      orderBy15: '',
      orderBy16: '',
      orderBy17: '',
      orderBy18: '',
      orderBy19: '',
      orderBy20: '',
      orderBy21: '',
      orderBy22: '',
      orderBy23: '',
      orderBy24: '',
      orderBy25: '',
      orderBy26: '',
      orderBy27: '',
      orderBy28: '',
      orderBy29: '',
      orderBy30: '',
      orderBy31: '',
      orderBy32: '',
      orderBy33: '',
      orderBy34: '',
      orderBy35: '',
      orderBy36: '',
      orderBy37: '',
      orderBy38: '',
      orderBy39: '',
      isSubmit: true, //防止重复提交
    },
    onLoad: function(e) {
        var openId = wx.getStorageSync('openId');
        var proNo = e.proNo;
        var proName = e.proName;
        this.setData({
            proNo: proNo,
            openId: openId
        })
        wx.setNavigationBarTitle({
            title: proName
        })
        console.log("proNo:" + proNo)
        this.getno();
        this.getProAttList();
    },
    //ocr名字赋值
    ocrNameInput: function(e) {
        this.setData({
            ocrName: e.detail.value
        })
    },
    //ocr性别赋值
    ocrSexInput: function(e) {
        this.setData({
            ocrSex: e.detail.value
        })
    },
    //ocr民族赋值
    ocrNationInput: function(e) {
        this.setData({
            ocrNation: e.detail.value
        })
    },
    //ocr出生赋值
    ocrBirthInput: function(e) {
        this.setData({
            ocrBirth: e.detail.value
        })
    },
    //ocr地址赋值
    ocrAddressInput: function(e) {
        this.setData({
            ocrAddress: e.detail.value
        })
    },
    //ocr身份证号
    ocrIdNumberInput: function(e) {
        this.setData({
            ocrIdNumber: e.detail.value
        })
    },
    //ocr发证机关
    ocrOfficeInput: function(e) {
        this.setData({
            ocrOffice: e.detail.value
        })
    },
    //ocr有效期限
    ocrValidityPeriodInput: function(e) {
        this.setData({
            ocrValidityPeriod: e.detail.value
        })
    },

  orderBy00Input: function (e) { this.setData({ orderBy00: e.detail.value }) },
  orderBy01Input: function (e) { this.setData({ orderBy01: e.detail.value }) },
  orderBy02Input: function (e) { this.setData({ orderBy02: e.detail.value }) },
  orderBy03Input: function (e) { this.setData({ orderBy03: e.detail.value }) },
  orderBy04Input: function (e) { this.setData({ orderBy04: e.detail.value }) },
  orderBy05Input: function (e) { this.setData({ orderBy05: e.detail.value }) },
  orderBy06Input: function (e) { this.setData({ orderBy06: e.detail.value }) },
  orderBy07Input: function (e) { this.setData({ orderBy07: e.detail.value }) },
  orderBy08Input: function (e) { this.setData({ orderBy08: e.detail.value }) }, 
  orderBy4Input: function (e) { this.setData({ orderBy4: e.detail.value }) },
  orderBy5Input: function (e) { this.setData({ orderBy5: e.detail.value }) },
  orderBy6Input: function (e) { this.setData({ orderBy6: e.detail.value }) },
  orderBy7Input: function (e) { this.setData({ orderBy7: e.detail.value }) },
  orderBy8Input: function (e) { this.setData({ orderBy8: e.detail.value }) },
  orderBy9Input: function (e) { this.setData({ orderBy9: e.detail.value }) },
  orderBy10Input: function (e) { this.setData({ orderBy10: e.detail.value }) },
  orderBy11Input: function (e) { this.setData({ orderBy11: e.detail.value }) },
  orderBy12Input: function (e) { this.setData({ orderBy12: e.detail.value }) },
  orderBy13Input: function (e) { this.setData({ orderBy13: e.detail.value }) },
  orderBy14Input: function (e) { this.setData({ orderBy14: e.detail.value }) },
  orderBy15Input: function (e) { this.setData({ orderBy15: e.detail.value }) },
  orderBy16Input: function (e) { this.setData({ orderBy16: e.detail.value }) },
  orderBy17Input: function (e) { this.setData({ orderBy17: e.detail.value }) },
  orderBy18Input: function (e) { this.setData({ orderBy18: e.detail.value }) },
  orderBy19Input: function (e) { this.setData({ orderBy19: e.detail.value }) },
  orderBy20Input: function (e) { this.setData({ orderBy20: e.detail.value }) },
  orderBy21Input: function (e) { this.setData({ orderBy21: e.detail.value }) },
  orderBy22Input: function (e) { this.setData({ orderBy22: e.detail.value }) },
  orderBy23Input: function (e) { this.setData({ orderBy23: e.detail.value }) },
  orderBy24Input: function (e) { this.setData({ orderBy24: e.detail.value }) },
  orderBy25Input: function (e) { this.setData({ orderBy25: e.detail.value }) },
  orderBy26Input: function (e) { this.setData({ orderBy26: e.detail.value }) },
  orderBy27Input: function (e) { this.setData({ orderBy27: e.detail.value }) },
  orderBy28Input: function (e) { this.setData({ orderBy28: e.detail.value }) },
  orderBy29Input: function (e) { this.setData({ orderBy29: e.detail.value }) },
  orderBy30Input: function (e) { this.setData({ orderBy30: e.detail.value }) },
  orderBy31Input: function (e) { this.setData({ orderBy31: e.detail.value }) },
  orderBy32Input: function (e) { this.setData({ orderBy32: e.detail.value }) },
  orderBy33Input: function (e) { this.setData({ orderBy33: e.detail.value }) },
  orderBy34Input: function (e) { this.setData({ orderBy34: e.detail.value }) },
  orderBy35Input: function (e) { this.setData({ orderBy35: e.detail.value }) },
  orderBy36Input: function (e) { this.setData({ orderBy36: e.detail.value }) },
  orderBy37Input: function (e) { this.setData({ orderBy37: e.detail.value }) },
  orderBy38Input: function (e) { this.setData({ orderBy38: e.detail.value }) },
  orderBy39Input: function (e) { this.setData({ orderBy39: e.detail.value }) }, 

    //获取orderNo
    getno: function() {
        var that = this;
        wx.request({
            url: pathUrl + '/rest/base/getno/' + that.data.proNo,
            success: function(res) {
                if (res.data.ok || res.data.data) {
                    that.setData({
                        orderNo: res.data.data
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '获取数据失败',
                        confirmColor: '#7aa6d1',
                        showCancel: false
                    })
                }
            }
        })
    },
    //获取上传附件列表
    getProAttList: function() {
        var that = this;
        wx.request({
            url: pathUrl + '/rest/fxjProMainController/' + that.data.proNo,
            success: function(res) {
                console.log(res.data)
                if (res.data.ok || res.data.data) {
                    var newProAttList = res.data.data.fxjProAttList;
                    for (var i = 0; i < newProAttList.length; i++) {
                        newProAttList[i].picList = [];
                    }
                    that.setData({
                        proAttList: newProAttList,
                        proIsdb: res.data.data.proIsdb
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '获取数据失败',
                        confirmColor: '#7aa6d1',
                        showCancel: false
                    })
                }
            }
        })
    },
    //上传附件图片
    getEnclosurePic: function(e) {
        console.log(e);
        var picIndex = e.currentTarget.dataset.index;
        var attType = e.currentTarget.dataset.atttype;
        var that = this;
        wx.chooseImage({
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
            success: function(res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
                var tempFilePaths = res.tempFilePaths;
                //启动上传等待中...  
                wx.showToast({
                    title: '正在上传...',
                    icon: 'loading',
                    mask: true,
                    duration: 10000
                })
                var uploadImgCount = 0;
                for (var i = 0, h = tempFilePaths.length; i < h; i++) {
                    wx.uploadFile({
                        url: pathUrl + '/rest/base/upload',
                        filePath: tempFilePaths[i],
                        name: 'uploadfile_ant',
                        formData: {
                            sbtype: 'NO',
                            filetype: attType,
                            orderNo: that.data.orderNo
                        },
                        header: {
                            "Content-Type": "multipart/form-data"
                        },
                        success: function(res) {
                            uploadImgCount++;
                            var data = JSON.parse(res.data);
                            var proAttListInfo = that.data.proAttList;

                            proAttListInfo[picIndex].picList.push({
                                picUrl: data[0].fileUrl,
                                picId: data[0].fileId
                            });
                            that.setData({
                                proAttList: proAttListInfo
                            });
                            if (uploadImgCount == tempFilePaths.length) {
                                wx.hideToast();
                            }
                        },
                        fail: function(res) {
                            wx.hideToast();
                            wx.showModal({
                                title: '错误提示',
                                content: '上传图片失败',
                                showCancel: false
                            })
                        }
                    });
                }
            }
        });
    },
    //删除上传附件图片
    deletePic: function(e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        var imgIdx = e.currentTarget.dataset.imgIdx;
        var deletPicId = that.data.proAttList[index].picList[imgIdx].picId;
        if (that.data.flag) {
            that.data.proAttList[index].picList.splice(imgIdx, 1);
            that.setData({
                proAttList: that.data.proAttList,
                flag: false
            })
        }
        wx.request({
            url: pathUrl + '/rest/base/delfile/' + deletPicId,
            success: function(res) {
                if (res.data.ok || res.data.data) {
                    that.setData({
                        flag: true
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '获取数据失败',
                        confirmColor: '#7aa6d1',
                        showCancel: false
                    })
                }
            }
        })
    },
    //上传OCR图片
    upEnclosure: function(e) {
        var that = this;
        var sbtype = e.currentTarget.dataset.sbtype;
        console.log(sbtype);
        wx.chooseImage({
            count: 1, //最多可以选择的图片总数  
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
            success: function(res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
                var tempFilePaths = res.tempFilePaths;
                //启动上传等待中...  
                wx.showToast({
                    title: '正在上传...',
                    icon: 'loading',
                    mask: true,
                    duration: 20000
                })
                var uploadImgCount = 0;
                for (var i = 0, h = tempFilePaths.length; i < h; i++) {
                    wx.uploadFile({
                        url: pathUrl + '/rest/base/uploadsb',
                        filePath: tempFilePaths[i],
                        name: 'uploadfile_ant',
                        formData: {
                            sbtype: sbtype,
                            filetype: 'sb',
                            orderNo: that.data.orderNo
                        },
                        header: {
                            "Content-Type": "multipart/form-data"
                        },
                        success: function(res) {
                            uploadImgCount++;
                            console.log(res);
                            if (sbtype == 2) {
                                var data = JSON.parse(res.data);
                                data = JSON.parse(data[0].piccontent);
                                var items = data.result.cardsinfo[0].items;
                                console.log(items)
                                that.setData({
                                    ocrName: items[1].content,
                                    ocrSex: items[2].content,
                                    ocrNation: items[3].content,
                                    ocrBirth: items[4].content,
                                    ocrAddress: items[5].content,
                                    ocrIdNumber: items[6].content,
                                })
                                console.log("sbtype == 2:" + JSON.stringify(items))
                            } else if (sbtype == 3) {
                                var data = JSON.parse(res.data);
                                data = JSON.parse(data[0].piccontent);
                                var items = data.result.cardsinfo[0].items;
                                that.setData({
                                    ocrOffice: items[1].content,
                                    ocrValidityPeriod: items[2].content,
                                    cusIdValidfrom: items[3].content,
                                    cusIdValidto: items[4].content
                                })
                                console.log("sbtype == 3:" + JSON.stringify(items))

                            } else if (sbtype == 6) {
                                var data = JSON.parse(res.data);
                                data = JSON.parse(data[0].piccontent);
                                var items = data.result.cardsinfo[0].items;
                                that.setData({
                                  brand: items[5].content,
                                  plateNumber: items[1].content,
                                  holder: items[3].content,
                                  dateIssue: items[8].content,
                                  nature: items[10].content,
                                  address: items[4].content,
                                  vinno: items[6].content,
                                  engineNumber: items[7].content,
                                })
                                console.log("sbtype == 6:" + JSON.stringify(items))
                            } else if (sbtype == 'yyzz'){
                                console.log('这是营业执照')
                               
                                var data = JSON.parse(res.data);
                                data = JSON.parse(data[0].piccontent);
                                // var items = data.result.cardsinfo[0].items;
                              var items = data;
                              console.log(JSON.stringify(data));
                              that.setData({

                                orderBy00: items.reg_num,
                                orderBy01: items.name,
                                orderBy02: items.type,
                                orderBy03: items.address,
                                orderBy04: items.person,
                                orderBy05: items.capital,
                                orderBy06: items.establish_date,
                                orderBy07: items.valid_period,
                                orderBy08: items.business,

                              
                              })
                                console.log(data)
                            } else if (sbtype == 'zzsfp'){
                                console.log('这是发票！')
                                var data = JSON.parse(res.data);
                                data = JSON.parse(data[0].piccontent);
                                // var items = data.result.cardsinfo[0].items;

                                console.log(data)
                            }
                            if (uploadImgCount == tempFilePaths.length) {
                                wx.hideToast();
                            }
                        },
                        fail: function(res) {
                            wx.hideToast();
                            wx.showModal({
                                title: '错误提示',
                                content: '上传图片失败',
                                showCancel: false
                            })
                        }
                    });
                }
            }
        });
    },
    // 提交
    submitBtn: function() {
        var that = this;
        if (that.data.isSubmit) {
            that.setData({
                isSubmit: false
            });
            var data = {
                bpmStatus: 10,
                createBy: that.data.openId,
                sysOrgCode: '',
                orderNo: that.data.orderNo, //订单编号
                proNo: that.data.proNo, //产品编号
                cusName: that.data.ocrName, //姓名
                cusGender: that.data.ocrSex, //性别
                cusEthnical: that.data.ocrNation, //民族
                cusBirthday: that.data.ocrBirth, //出生日期
                cusResAddr: that.data.ocrAddress, //地址
                cusrIdtype: '身份证', //身份证为固定值
                cusIncard: that.data.ocrIdNumber, // 身份证号
                createSysorg: that.data.ocrOffice, //签发机关
                cusIdValidfrom: that.data.cusIdValidfrom, //证件开始日期
                cusIdValidto: that.data.cusIdValidto, //证件结束日期

                orderCartype: that.data.brand, //车型 如果是房子就是面积
                orderCarno: that.data.plateNumber, // 车牌  如果是房子就是小区名称
                cusRemark: that.data.residentialAddress, // 个人资料的居住地址
                cusMobile: that.data.telephone, //电话
                cusMaritalStatus: that.data.checkedValue, //婚姻状况
                cusDeu: '', //学历
                cusAge: '',
                cusQqid: '', //qq号
                orderBy1: that.data.guaranteeName, //担保人姓名
                orderBy2: that.data.guaranteeIdCard, //担保人身份证号
                orderBy3: that.data.guaranteePhone, //担保人电话
                orderBy4: that.data.orderBy4, //担保人姓名
                orderBy5: that.data.orderBy5, //担保人身份证号
                orderBy6: that.data.orderBy6, //担保人电话
                orderBy7: that.data.orderBy7, //担保人姓名
                orderBy8: that.data.orderBy8, //担保人身份证号
                orderAmount: that.data.orderAmount, // 金额
                orderPeriod: that.data.orderPeriod, // 期数
                fxjOrderEmerList: [{
                    emergencyRelation: '', //关系
                    emergencyContact: that.data.spouseName, //姓名
                    emergencyMobile: that.data.spouseTelephone, //电话
                    emergencyOffice: '',
                    emergencyResAddr: '' //地址
                }, {
                    emergencyRelation: '', //关系
                    emergencyContact: that.data.urgent, //姓名
                    emergencyMobile: that.data.urgentTelephone, //电话
                    emergencyOffice: '',
                    emergencyResAddr: '' //地址
                }, {
                    emergencyRelation: '', //关系
                    emergencyContact: that.data.urgent2, //姓名
                    emergencyMobile: that.data.urgentTelephone2, //电话
                    emergencyOffice: '',
                    emergencyResAddr: '' //地址
                }],
                fxjOrderLoanList: [{
                    elementNo: 10,
                  elementContent: that.data.orderBy00
                }, {
                    elementNo: 20,
                    elementContent: that.data.orderBy01
                }, {
                    elementNo: 30,
                    elementContent: that.data.orderBy02
                }, {
                    elementNo: 40,
                    elementContent: that.data.orderBy03
                }, {
                    elementNo: 50,
                    elementContent: that.data.orderBy04
                }, {
                    elementNo: 60,
                    elementContent: that.data.orderBy05
                }, {
                    elementNo: 70,
                    elementContent: that.data.orderBy06
                }, {
                    elementNo: 80,
                    elementContent: that.data.orderBy07
                  }, {
                    elementNo: 90,
                    elementContent: that.data.orderBy08
                  }, { elementNo: 910, elementContent: that.data.orderBy4 }
                  , { elementNo: 911, elementContent: that.data.orderBy5 }
                  , { elementNo: 912, elementContent: that.data.orderBy6 }
                  , { elementNo: 913, elementContent: that.data.orderBy7 }
                  , { elementNo: 914, elementContent: that.data.orderBy8 }
                  , { elementNo: 915, elementContent: that.data.orderBy9 }
                  , { elementNo: 916, elementContent: that.data.orderBy10 }
                  , { elementNo: 917, elementContent: that.data.orderBy11 }
                  , { elementNo: 918, elementContent: that.data.orderBy12 }
                  , { elementNo: 919, elementContent: that.data.orderBy13 }
                  , { elementNo: 920, elementContent: that.data.orderBy14 }
                  , { elementNo: 921, elementContent: that.data.orderBy15 }
                  , { elementNo: 922, elementContent: that.data.orderBy16 }
                  , { elementNo: 923, elementContent: that.data.orderBy17 }
                  , { elementNo: 924, elementContent: that.data.orderBy18 }
                  , { elementNo: 925, elementContent: that.data.orderBy19 }
                  , { elementNo: 926, elementContent: that.data.orderBy20 }
                  , { elementNo: 927, elementContent: that.data.orderBy21 }
                  , { elementNo: 928, elementContent: that.data.orderBy22 }
                  , { elementNo: 929, elementContent: that.data.orderBy23 }
                  , { elementNo: 930, elementContent: that.data.orderBy24 }
                  , { elementNo: 931, elementContent: that.data.orderBy25 }
                  , { elementNo: 932, elementContent: that.data.orderBy26 }
                  , { elementNo: 933, elementContent: that.data.orderBy27 }
                  , { elementNo: 934, elementContent: that.data.orderBy28 }
                  , { elementNo: 935, elementContent: that.data.orderBy29 }
                  , { elementNo: 936, elementContent: that.data.orderBy30 }
                  , { elementNo: 937, elementContent: that.data.orderBy31 }
                  , { elementNo: 938, elementContent: that.data.orderBy32 }
                  , { elementNo: 939, elementContent: that.data.orderBy33 }
                  , { elementNo: 940, elementContent: that.data.orderBy34 }
                  , { elementNo: 941, elementContent: that.data.orderBy35 }
                  , { elementNo: 942, elementContent: that.data.orderBy36 }
                  , { elementNo: 943, elementContent: that.data.orderBy37 }]
            };
            console.log("data:" + JSON.stringify(data));
            wx.request({
                url: pathUrl + '/rest/fxjOrderMainController/' + that.data.openId,
                data: data,
                method: 'POST',
                success: function(res) {
                    console.log(res.data)
                    if (res.data.ok) {
                        that.setData({
                            isSubmit: true
                        });
                        wx.showToast({
                            title: '提交成功',
                            duration: 1000,
                            mask: true,
                            success: function() {
                                setTimeout(function() {
                                    wx.switchTab({
                                        url: "/pages/index/index"
                                    })
                                }, 1000);
                            }
                        })
                    } else {
                        that.setData({
                            isSubmit: true
                        });
                        wx.showToast({
                            title: '提交失败',
                            icon: 'none',
                            mask: true,
                            duration: 1000
                        })
                    }
                }
            })
        } else {
            wx.showToast({
                title: '请勿重复提交',
                icon: 'none',
                mask: true,
                duration: 1000
            })
        }
    },

    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})